﻿using Ledict.ConditionExtract.BLL.WorkProcess;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.DAL;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Ledict.ConditionExtract.BLL
{
    public class WorkThread_first_PeopleNew : WorkThread_first
    {

        public int MaxCount = 0;//处理总数
        private int CurrentCount = 0;//处理当前数量

        static StringBuilder stb = new StringBuilder();
        protected TreeDataBLL trbll = new TreeDataBLL();
        protected Ledict.ConditionExtract.BLL.Express_AddElement addelementbll = new Express_AddElement();
        protected ExpressGroupSituationBLL expresitu = new ExpressGroupSituationBLL();
        protected Express_MatchSituationBLL match = new Express_MatchSituationBLL();
        protected AboutRegexBLL aboutreg = new AboutRegexBLL();
        protected RegexParse.RegexParse getregex = new RegexParse.RegexParse();
        protected ProcessDataSourceBLL datas = new ProcessDataSourceBLL();
        protected WorkThread_Public work = new WorkThread_Public();
        protected XmlLabel_LabelRelBLL xmlLabel_LabelRelBLL = new XmlLabel_LabelRelBLL();


        protected Ledict.ConditionExtract.BLL.Express_WordBLL wordbll = new Express_WordBLL();

        CaseCauseBLL casecausebll = new CaseCauseBLL();
        StaticCacheBLL staticcache = new StaticCacheBLL();

        Express_SourceBLL expressSourcebll = new Express_SourceBLL();
        Express_MatchSituationBLL MatchSituationbll = new Express_MatchSituationBLL();
        DataTable expressWordData = new DataTable();
        NewIDBLL newid = new NewIDBLL();
        //存放该节点下面的子节点集合
        public DataTable caseTable = new DataTable();
        //存放正则信息单元集合
        public DataTable regexCodeTable = new DataTable();

        #region 加工处理(核心)

        /// <summary>
        /// 开始加工（一个案由）
        /// </summary>
        public override void execute_Start()
        {
            try
            {
                //打开缓存
                getregex.StartCache();
                //查询案由的案由路径供索引查询使用
                conditon.HeadwordId = casecausebll.GetAllParentIdByCCID(conditon.HeadwordId);
                MaxCount = 0;
                CurrentCount = 0;
                //删除表述的内容 未打勾的
                wordbll.DeleteSYS_Express_Word_Process(conditon.SituationId);
                //删除表述关系 未打勾的
                MatchSituationbll.DeleteSYS_Express_MatchSituation(conditon.SituationId);
                //2、待处理的数据
                //包含本节点 从索引中取
                if (/*conditon.SituationName == "争议焦点要素" &&*/ conditon.IsHasOwnNode)
                {
                    MaxCount += GetDataSourceCount(conditon);
                }//不包含本节点 从数据库中取数据
                //else if (/*conditon.SituationName == "争议焦点要素" &&*/ !conditon.IsHasOwnNode)
                //{

                //    expressWordData = MatchSituationbll.GetExpressWordData(conditon.SituationId);
                //    MaxCount += expressWordData.Rows.Count;
                //}
                else
                {
                    //从数据库中取数据
                    expressWordData = MatchSituationbll.GetExpressWordData(casecausebll.GetParentIDByID(conditon.SituationId));
                    MaxCount += expressWordData.Rows.Count;
                }
                // 没数据结束进度
                if (MaxCount == 0)   //没有可以跑数据的案例
                {
                    if (MaxCount >= CurrentCount)
                    {
                        processShow(MaxCount, CurrentCount);
                    }
                }
                else//加工数据
                {
                    execute_OneCondition(conditon);
                }
                //关闭缓存
                getregex.EndCache();
            }
            catch (Exception ex)
            {
               //throw new Exception(ex.Message);
                Log.WriteLog("execute_Start发生错误：" + ex.Message);
            }

        }

        List<string> dic = new List<string>();
        // Dictionary<string,Ew> dic=new Dictionary<string, Ew>(
        /// <summary>
        /// （处理具体一个案由）
        /// </summary>
        /// <param name="conditon"></param>
        public virtual void execute_OneCondition1(DataSourceCondition conditon)//案由ID
        {
            try
            {
                int PerPage = 5000;//每次5000条
                int page = (int)Math.Ceiling((double)MaxCount / PerPage);//多少页

                DataTable Caseiddt;//分页数据源集合  
                                   //首先将争议焦点要素插入SYS_Express_MatchSituation表,如果是争议焦点要素并且包含本节点，需要新建节点     
                if (conditon.SituationName == "争议焦点要素" && conditon.IsHasOwnNode)
                {
                    //先清除节点下面的数据
                    MatchSituationbll.DeleteSys_ExpressAndWordZhengYI(conditon.SituationId);
                    //wordbll.InsertDeleteLog("SYS_Express_Word_Process", FrmLogin.userName, conditon.SituationId);

                    //判断msID是否存在，如果不存在就创建，存在就获取msID
                    string msID = MatchSituationbll.ExistsMatchSituationByCCID(conditon.SituationId);
                    if (string.IsNullOrEmpty(msID))
                    {
                        SYS_Express_MatchSituation stiuModel = new SYS_Express_MatchSituation();
                        stiuModel.MS_ID = newid.JudgeDoc_CreateID("SYS_Express_MatchSituation");//Guid.NewGuid().ToString();
                        msID = stiuModel.MS_ID;
                        stiuModel.SituationID = conditon.SituationId;
                        DataTable dtStiuid = casecausebll.GetSYS_CaseCauseByCCID(conditon.SituationId);
                        stiuModel.SituationType = Convert.ToInt32(dtStiuid.Rows[0]["CC_NodeType"]);
                        stiuModel.SituationName = dtStiuid.Rows[0]["CC_Name"].ToString();
                        stiuModel.ParentID = dtStiuid.Rows[0]["CC_ParentID"].ToString();
                        stiuModel.AllStituationIDs = conditon.SituationId;
                        stiuModel.AllParentID = dtStiuid.Rows[0]["CC_ParentID"].ToString() + "," + conditon.SituationId;
                        stiuModel.MatchSource = "0";
                        stiuModel.CC_IsShowExpress = 1;
                        MatchSituationbll.AddMatchSituation(stiuModel);//添加
                    }
                    else
                    {
                        for (int pag = 1; pag <= page; pag++)//分页
                        {
                            Caseiddt = GetDataSource(conditon, pag, PerPage);//案由ID的相关文本数据(得到Caseid)
                            string res = expressSourcebll.GetRegexInfoCodeByExpressSource(conditon.SituationId);
                            //string[] str = res.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                            //for (int i = 0; i < Caseiddt.Rows.Count; i++)
                            //{
                            //    int k = 0;
                            //    StringBuilder sb = new StringBuilder();
                            //    if (Caseiddt.Rows[i]["ParagraphText"].ToString().Contains("争议焦点"))
                            //    {
                            //        k++;
                            //        sb.Append(Caseiddt.Rows[i]["ParagraphText"].ToString());
                            //        bool res1 = getregex.IsMatch(res, Caseiddt.Rows[i]["ParagraphText"].ToString());
                            //    }
                            //}
                            for (int j = 0; j < Caseiddt.Rows.Count; j++)
                            {
                                //获取该节点对应的信息单元                               
                                //匹配
                                try
                                {
                                    if (getregex.IsMatch(res, Caseiddt.Rows[j]["ParagraphText"].ToString()))
                                    {
                                        //匹配数据后首先根据当前节点进行插入数据
                                        SYS_Express_Word WordModel = new SYS_Express_Word();
                                        WordModel.MS_ID = msID;
                                        WordModel.EW_CaseId = Caseiddt.Rows[j]["CaseId"].ToString();
                                        WordModel.EW_Content = Caseiddt.Rows[j]["ParagraphText"].ToString();
                                        WordModel.EW_UseCount = 1;
                                        WordModel.EW_WordCount = Caseiddt.Rows[j]["ParagraphText"].ToString().Length;
                                        WordModel.ParagraphTypeId = Convert.ToInt32(Caseiddt.Rows[j]["ParagraphTypeId"].ToString());
                                        WordModel.EW_ID = wordbll.Exists_WordProcess1(WordModel);
                                        WordModel.EW_IsSingleExpress = 1;
                                        if (WordModel.EW_ID == "")
                                        {
                                            WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                                            WordModel.EW_Right = 0;
                                            WordModel.EW_Wrong = 0;
                                            wordbll.AddWord_Process(WordModel);
                                        }
                                        else
                                        {
                                            CurrentCount++; // 显示进度条
                                            if (MaxCount >= CurrentCount)
                                            {
                                                processShow(MaxCount, CurrentCount);
                                            }
                                            lockObj.WaitOne();
                                            if (readyToClose)
                                            {
                                            }
                                            continue;
                                        }
                                        //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）

                                        computerNode(WordModel.EW_ID, conditon.SituationId, Caseiddt.Rows[j]["ParagraphText"].ToString(), WordModel.EW_CaseId);
                                    }
                                    CurrentCount++; // 显示进度条
                                    if (MaxCount >= CurrentCount)
                                    {
                                        processShow(MaxCount, CurrentCount);
                                    }
                                    lockObj.WaitOne();
                                    if (readyToClose)
                                    {
                                    }
                                }
                                catch (Exception ex)
                                {

                                    throw new Exception(ex.Message);
                                }
                            }
                        }
                    }
                }
                else if (conditon.SituationName == "争议焦点要素" && !conditon.IsHasOwnNode)
                {
                    //先获取数据然后再删除
                    expressWordData = MatchSituationbll.GetExpressWordData(conditon.SituationId);
                    MatchSituationbll.DeleteSys_ExpressAndWordZhengYI(conditon.SituationId);
                    string msID = MatchSituationbll.ExistsMatchSituationByCCID(conditon.SituationId);
                    DataTable dt = casecausebll.Select_parentNode_child(conditon.SituationId);
                    for (int j = 0; j < expressWordData.Rows.Count; j++)
                    {
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            string res = expressSourcebll.GetRegexInfoCodeByExpressSource(dt.Rows[k]["CC_ID"].ToString());
                            //判断是否匹配
                            if (getregex.IsMatch(res, expressWordData.Rows[j]["EW_Content"].ToString()))
                            {
                                //匹配数据后首先根据当前节点进行插入数据
                                SYS_Express_Word WordModel = new SYS_Express_Word();
                                WordModel.MS_ID = msID;
                                WordModel.EW_CaseId = expressWordData.Rows[j]["EW_CaseId"].ToString();
                                WordModel.EW_Content = expressWordData.Rows[j]["EW_Content"].ToString();
                                WordModel.EW_UseCount = 1;
                                WordModel.EW_WordCount = expressWordData.Rows[j]["EW_Content"].ToString().Length;
                                WordModel.ParagraphTypeId = Convert.ToInt32(expressWordData.Rows[j]["ParagraphTypeId"].ToString());
                                WordModel.EW_ID = wordbll.Exists_WordProcess(WordModel);
                                WordModel.EW_IsSingleExpress = 1;
                                if (WordModel.EW_ID == "")
                                {
                                    WordModel.MS_ID = dt.Rows[k]["CC_ID"].ToString();
                                    WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                                    WordModel.EW_Right = 0;
                                    WordModel.EW_Wrong = 0;
                                    wordbll.AddWord_Process(WordModel);
                                }
                                else
                                {
                                    CurrentCount++; // 显示进度条
                                    if (MaxCount >= CurrentCount)
                                    {
                                        processShow(MaxCount, CurrentCount);
                                    }
                                    lockObj.WaitOne();
                                    if (readyToClose)
                                    {
                                    }
                                    continue;
                                }
                                //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）

                                computerNode(WordModel.EW_ID, dt.Rows[k]["CC_ID"].ToString(), expressWordData.Rows[j]["EW_Content"].ToString(), WordModel.EW_CaseId);
                            }
                        }
                        CurrentCount++; // 显示进度条
                        if (MaxCount >= CurrentCount)
                        {
                            processShow(MaxCount, CurrentCount);
                        }
                        lockObj.WaitOne();
                        if (readyToClose)
                        {
                        }
                    }
                }
                else
                {
                    //先获取数据然后再删除
                    expressWordData = MatchSituationbll.GetExpressWordData(casecausebll.GetParentIDByID(conditon.SituationId));
                    MatchSituationbll.DeleteSys_ExpressNotZhengYi(casecausebll.GetParentIDByID(conditon.SituationId));
                    string msID = MatchSituationbll.ExistsMatchSituationByCCID(casecausebll.GetParentIDByID(conditon.SituationId));
                    // DataTable dt = casecausebll.Select_parentNode_child(casecausebll.GetParentIDByID(conditon.SituationId));
                    string res = expressSourcebll.GetRegexInfoCodeByExpressSource(casecausebll.GetParentIDByID(conditon.SituationId));
                    for (int j = 0; j < expressWordData.Rows.Count; j++)
                    {
                        //for (int k = 0; k < dt.Rows.Count; k++)
                        //{

                        //判断是否匹配
                        if (getregex.IsMatch(res, expressWordData.Rows[j]["EW_Content"].ToString()))
                        {
                            //匹配数据后首先根据当前节点进行插入数据
                            SYS_Express_Word WordModel = new SYS_Express_Word();
                            WordModel.MS_ID = string.IsNullOrEmpty(msID) ? casecausebll.GetParentIDByID(conditon.SituationId) : msID;
                            WordModel.EW_CaseId = expressWordData.Rows[j]["EW_CaseId"].ToString();
                            WordModel.EW_Content = expressWordData.Rows[j]["EW_Content"].ToString();
                            WordModel.EW_UseCount = 1;
                            WordModel.EW_WordCount = expressWordData.Rows[j]["EW_Content"].ToString().Length;
                            WordModel.ParagraphTypeId = Convert.ToInt32(expressWordData.Rows[j]["ParagraphTypeId"].ToString());
                            WordModel.EW_ID = wordbll.Exists_WordProcess(WordModel);
                            WordModel.EW_IsSingleExpress = 1;
                            if (WordModel.EW_ID == "")
                            {
                                WordModel.MS_ID = casecausebll.GetParentIDByID(conditon.SituationId);
                                WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                                WordModel.EW_Right = 0;
                                WordModel.EW_Wrong = 0;
                                wordbll.AddWord_Process(WordModel);
                            }
                            else
                            {
                                CurrentCount++; // 显示进度条
                                if (MaxCount >= CurrentCount)
                                {
                                    processShow(MaxCount, CurrentCount);
                                }
                                lockObj.WaitOne();
                                if (readyToClose)
                                {
                                }
                                continue;
                            }
                            //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）

                            computerNode(WordModel.EW_ID, casecausebll.GetParentIDByID(conditon.SituationId), expressWordData.Rows[j]["EW_Content"].ToString(), WordModel.EW_CaseId);
                        }
                        CurrentCount++; // 显示进度条
                        if (MaxCount >= CurrentCount)
                        {
                            processShow(MaxCount, CurrentCount);
                        }
                        lockObj.WaitOne();
                        if (readyToClose)
                        {
                        }
                    }

                }
                //  }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// 加工数据
        /// </summary>
        /// <param name="conditon"></param>
        public virtual void execute_OneCondition(DataSourceCondition conditon)//案由ID
        {
            try
            {
                int PerPage = 5000;//每次5000条
                int page = (int)Math.Ceiling((double)MaxCount / PerPage);//多少页

                DataTable Caseiddt;//分页数据源集合  
                 //争议焦点要素包含本节点
                if (conditon.SituationName == "争议焦点要素")
                {
                    //先清除节点下面的数据
                    if (conditon.IsHasOwnNode)
                    {
                        Log.WriteLog("表述工具新民事处理处理数据开始" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                        for (int pag = 1; pag <= page; pag++)//分页
                        {
                            Log.WriteLog("开始处理第" + pag + "页数据");
                            Caseiddt = GetDataSource(conditon, pag, PerPage);//案由ID的相关文本数据(得到Caseid)
                            Caseiddt = PublicMethod.GetDistinctTable(Caseiddt, new string[] { "CaseId", "ParagraphTypeId", "ParagraphText" });
                            Log.WriteLog("获取到第一页数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                            //  string res = expressSourcebll.GetRegexInfoCodeByExpressSource(conditon.SituationId);
                            //获取所有节点集合
                            caseTable = casecausebll.GetAllChild1(conditon.SituationId);
                            regexCodeTable = expressSourcebll.GetRegexInfoCodeByCCID(conditon.SituationId,conditon.OperatePurposeId,conditon.OperateParagraphId);
                            int kkk = 0;
                            if (pag == page)
                            {
                                kkk = MaxCount - (5000 * (pag - 1)) - Caseiddt.Rows.Count;
                            }
                            else
                            {
                                kkk = 5000 - Caseiddt.Rows.Count;
                            }
                            for (int j = 0; j < Caseiddt.Rows.Count; j++)
                            {
                                //获取该节点对应的信息单元                               
                                //匹配
                                try
                                {

                                    //if (!dic.Contains(Caseiddt.Rows[j]["CaseId"].ToString()))
                                    //{
                                    //    dic.Add(Caseiddt.Rows[j]["CaseId"].ToString());

                                    DataRow[] dr = regexCodeTable.Select(" CC_ID ='" + conditon.SituationId + "'");
                                    if (dr.Length > 0)
                                    {
                                        for (int k = 0; k < dr.Length; k++)
                                        {
                                            if (getregex.IsMatch(dr[k]["CC_RegexInfoCode"].ToString(), Caseiddt.Rows[j]["ParagraphText"].ToString()))
                                            {
                                                k = dr.Length;
                                                Log.WriteLog("处理第" + j + "条数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                                                DateTime dtt = DateTime.Now;
                                                //匹配数据后首先根据当前节点进行插入数据
                                                SYS_Express_Word WordModel = new SYS_Express_Word();
                                                WordModel.EW_CaseId = Caseiddt.Rows[j]["CaseId"].ToString();
                                                WordModel.EW_Content = Caseiddt.Rows[j]["ParagraphText"].ToString();
                                                WordModel.EW_UseCount = 1;
                                                WordModel.EW_WordCount = Caseiddt.Rows[j]["ParagraphText"].ToString().Length;
                                                WordModel.ParagraphTypeId = Convert.ToInt32(Caseiddt.Rows[j]["ParagraphTypeId"].ToString());
                                                WordModel.EW_Right = 0;
                                                WordModel.EW_Wrong = 0;
                                                computerNode(WordModel, conditon.SituationId, Caseiddt.Rows[j]["ParagraphText"].ToString());
                                                //wordbll.AddWord_Process(WordModel);
                                                CurrentCount++; // 显示进度条
                                                if (j + 1 == Caseiddt.Rows.Count)
                                                {
                                                    CurrentCount += kkk;
                                                }
                                                if (MaxCount >= CurrentCount)
                                                {
                                                    processShow(MaxCount, CurrentCount);
                                                }
                                                lockObj.WaitOne();
                                                if (readyToClose)
                                                {
                                                }
                                                TimeSpan ts = DateTime.Now - dtt;
                                                Log.WriteLog("处理第" + j + "条数据完毕" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 耗时：" + ts.Seconds + "秒");
                                                continue;
                                                //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）
                                            }
                                            else
                                            {
                                                CurrentCount++; // 显示进度条
                                                if (j + 1 == Caseiddt.Rows.Count)
                                                {
                                                    CurrentCount += kkk;
                                                }
                                                if (MaxCount >= CurrentCount)
                                                {
                                                    processShow(MaxCount, CurrentCount);
                                                }
                                                lockObj.WaitOne();
                                                if (readyToClose)
                                                {
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {

                                        CurrentCount++; // 显示进度条
                                        if (j + 1 == Caseiddt.Rows.Count)
                                        {
                                            CurrentCount += kkk;
                                        }
                                        if (MaxCount >= CurrentCount)
                                        {
                                            processShow(MaxCount, CurrentCount);
                                        }
                                        lockObj.WaitOne();
                                        if (readyToClose)
                                        {
                                        }
                                    }
                                    //}
                                    //else
                                    //{
                                    //    CurrentCount++; // 显示进度条
                                    //    if (j + 1 == Caseiddt.Rows.Count)
                                    //    {
                                    //        CurrentCount += kkk;
                                    //    }
                                    //    if (MaxCount >= CurrentCount)
                                    //    {
                                    //        processShow(MaxCount, CurrentCount);
                                    //    }
                                    //    lockObj.WaitOne();
                                    //    if (readyToClose)
                                    //    {
                                    //    }
                                    //}
                                }
                                catch (Exception ex)
                                {
                                    Log.WriteLog("争议焦点要素发生错误：" + ex.Message);
                                    throw new Exception(ex.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        Log.WriteLog("表述工具新民事处理处理数据开始" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                        Caseiddt = MatchSituationbll.GetExpressWordData(conditon.SituationId);
                            //获取所有节点集合
                            caseTable = casecausebll.GetAllChild1(conditon.SituationId);
                            regexCodeTable = expressSourcebll.GetRegexInfoCodeByCCID(conditon.SituationId);                           
                            for (int j = 0; j < Caseiddt.Rows.Count; j++)
                            {
                                //获取该节点对应的信息单元                               
                                //匹配
                                try
                                {

                                    //if (!dic.Contains(Caseiddt.Rows[j]["CaseId"].ToString()))
                                    //{
                                    //    dic.Add(Caseiddt.Rows[j]["CaseId"].ToString());

                                    DataRow[] dr = regexCodeTable.Select(" CC_ID ='" + conditon.SituationId + "'");
                                    if (dr.Length > 0)
                                    {
                                        for (int k = 0; k < dr.Length; k++)
                                        {
                                            if (getregex.IsMatch(dr[k]["CC_RegexInfoCode"].ToString(), Caseiddt.Rows[j]["ParagraphText"].ToString()))
                                            {
                                                k = dr.Length;
                                                Log.WriteLog("处理第" + j + "条数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                                                DateTime dtt = DateTime.Now;
                                                //匹配数据后首先根据当前节点进行插入数据
                                                SYS_Express_Word WordModel = new SYS_Express_Word();
                                                WordModel.EW_CaseId = Caseiddt.Rows[j]["CaseId"].ToString();
                                                WordModel.EW_Content = Caseiddt.Rows[j]["ParagraphText"].ToString();
                                                WordModel.EW_UseCount = 1;
                                                WordModel.EW_WordCount = Caseiddt.Rows[j]["ParagraphText"].ToString().Length;
                                                WordModel.ParagraphTypeId = Convert.ToInt32(Caseiddt.Rows[j]["ParagraphTypeId"].ToString());
                                                WordModel.EW_Right = 0;
                                                WordModel.EW_Wrong = 0;
                                                computerNode(WordModel, conditon.SituationId, Caseiddt.Rows[j]["ParagraphText"].ToString());
                                                //wordbll.AddWord_Process(WordModel);
                                                CurrentCount++; // 显示进度条
                                               
                                                if (MaxCount >= CurrentCount)
                                                {
                                                    processShow(MaxCount, CurrentCount);
                                                }
                                                lockObj.WaitOne();
                                                if (readyToClose)
                                                {
                                                }
                                                TimeSpan ts = DateTime.Now - dtt;
                                                Log.WriteLog("处理第" + j + "条数据完毕" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 耗时：" + ts.Seconds + "秒");
                                                continue;
                                                //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）
                                            }
                                            else
                                            {
                                                CurrentCount++; // 显示进度条                                               
                                                if (MaxCount >= CurrentCount)
                                                {
                                                    processShow(MaxCount, CurrentCount);
                                                }
                                                lockObj.WaitOne();
                                                if (readyToClose)
                                                {
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {

                                        CurrentCount++; // 显示进度条                                        
                                        if (MaxCount >= CurrentCount)
                                        {
                                            processShow(MaxCount, CurrentCount);
                                        }
                                        lockObj.WaitOne();
                                        if (readyToClose)
                                        {
                                        }
                                    }
                                    //}
                                    //else
                                    //{
                                    //    CurrentCount++; // 显示进度条
                                    //    if (j + 1 == Caseiddt.Rows.Count)
                                    //    {
                                    //        CurrentCount += kkk;
                                    //    }
                                    //    if (MaxCount >= CurrentCount)
                                    //    {
                                    //        processShow(MaxCount, CurrentCount);
                                    //    }
                                    //    lockObj.WaitOne();
                                    //    if (readyToClose)
                                    //    {
                                    //    }
                                    //}
                                }
                                catch (Exception ex)
                                {
                                    throw new Exception(ex.Message);
                                }
                            }
                    }                 
                }//理由要素包含本节点
                else if (conditon.SituationName == "理由要素" )
                {
                    //先清除节点下面的数据              
                    if (conditon.IsHasOwnNode)
                    {
                        Log.WriteLog("表述工具新民事处理处理数据开始" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                        //dic.Clear();
                        for (int pag = 1; pag <= page; pag++)//分页
                        {
                            Log.WriteLog("开始处理第" + pag + "页数据");
                            Caseiddt = GetDataSource(conditon, pag, PerPage);//案由ID的相关文本数据(得到Caseid)
                            Caseiddt = PublicMethod.GetDistinctTable(Caseiddt, new string[] { "CaseId", "ParagraphTypeId", "ParagraphText" });
                            Log.WriteLog("获取到第一页数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                            //  string res = expressSourcebll.GetRegexInfoCodeByExpressSource(conditon.SituationId);
                            //获取所有节点集合
                            caseTable = casecausebll.GetAllChild1(conditon.SituationId);
                            regexCodeTable = expressSourcebll.GetRegexInfoCodeByCCID(conditon.SituationId);
                            int kkk = 0;
                            if (pag == page)
                            {
                                kkk = MaxCount - (5000 * (pag - 1)) - Caseiddt.Rows.Count;
                            }
                            else
                            {
                                kkk = 5000 - Caseiddt.Rows.Count;
                            }
                            for (int j = 0; j < Caseiddt.Rows.Count; j++)
                            {
                                //获取该节点对应的信息单元                               
                                //匹配
                                try
                                {

                                    //if (!dic.Contains(Caseiddt.Rows[j]["CaseId"].ToString()))
                                    //{
                                    //    dic.Add(Caseiddt.Rows[j]["CaseId"].ToString());

                                    DataRow[] dr = regexCodeTable.Select(" CC_ID ='" + conditon.SituationId + "'");
                                    if (dr.Length > 0)
                                    {
                                        for (int k = 0; k < dr.Length; k++)
                                        {
                                            if (getregex.IsMatch(dr[k]["CC_RegexInfoCode"].ToString(), Caseiddt.Rows[j]["ParagraphText"].ToString()))
                                            {
                                                k = dr.Length;
                                                Log.WriteLog("处理第" + j + "条数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                                                DateTime dtt = DateTime.Now;
                                                //匹配数据后首先根据当前节点进行插入数据
                                                SYS_Express_Word WordModel = new SYS_Express_Word();
                                                WordModel.EW_CaseId = Caseiddt.Rows[j]["CaseId"].ToString();
                                                WordModel.EW_Content = Caseiddt.Rows[j]["ParagraphText"].ToString();
                                                WordModel.EW_UseCount = 1;
                                                WordModel.EW_WordCount = Caseiddt.Rows[j]["ParagraphText"].ToString().Length;
                                                WordModel.ParagraphTypeId = Convert.ToInt32(Caseiddt.Rows[j]["ParagraphTypeId"].ToString());
                                                WordModel.EW_Right = 0;
                                                WordModel.EW_Wrong = 0;
                                                computerNode(WordModel, conditon.SituationId, Caseiddt.Rows[j]["ParagraphText"].ToString());
                                                //wordbll.AddWord_Process(WordModel);
                                                CurrentCount++; // 显示进度条
                                                if (j + 1 == Caseiddt.Rows.Count)
                                                {
                                                    CurrentCount += kkk;
                                                }
                                                if (MaxCount >= CurrentCount)
                                                {
                                                    processShow(MaxCount, CurrentCount);
                                                }
                                                lockObj.WaitOne();
                                                if (readyToClose)
                                                {
                                                }
                                                TimeSpan ts = DateTime.Now - dtt;
                                                Log.WriteLog("处理第" + j + "条数据完毕" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 耗时：" + ts.Seconds + "秒");
                                                continue;
                                                //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）
                                            }
                                            else
                                            {
                                                CurrentCount++; // 显示进度条
                                                if (j + 1 == Caseiddt.Rows.Count)
                                                {
                                                    CurrentCount += kkk;
                                                }
                                                if (MaxCount >= CurrentCount)
                                                {
                                                    processShow(MaxCount, CurrentCount);
                                                }
                                                lockObj.WaitOne();
                                                if (readyToClose)
                                                {
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {

                                        CurrentCount++; // 显示进度条
                                        if (j + 1 == Caseiddt.Rows.Count)
                                        {
                                            CurrentCount += kkk;
                                        }
                                        if (MaxCount >= CurrentCount)
                                        {
                                            processShow(MaxCount, CurrentCount);
                                        }
                                        lockObj.WaitOne();
                                        if (readyToClose)
                                        {
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    throw new Exception(ex.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        Caseiddt = MatchSituationbll.GetExpressWordData(conditon.SituationId);

                        Log.WriteLog("获取到第一页数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                        //  string res = expressSourcebll.GetRegexInfoCodeByExpressSource(conditon.SituationId);
                        //获取所有节点集合
                        caseTable = casecausebll.GetAllChild1(conditon.SituationId);
                        regexCodeTable = expressSourcebll.GetRegexInfoCodeByCCID(conditon.SituationId);                      
                        for (int j = 0; j < Caseiddt.Rows.Count; j++)
                        {
                            //获取该节点对应的信息单元                               
                            //匹配
                            try
                            {

                                //if (!dic.Contains(Caseiddt.Rows[j]["CaseId"].ToString()))
                                //{
                                //    dic.Add(Caseiddt.Rows[j]["CaseId"].ToString());

                                DataRow[] dr = regexCodeTable.Select(" CC_ID ='" + conditon.SituationId + "'");
                                if (dr.Length > 0)
                                {
                                    for (int k = 0; k < dr.Length; k++)
                                    {
                                        if (getregex.IsMatch(dr[k]["CC_RegexInfoCode"].ToString(), Caseiddt.Rows[j]["ParagraphText"].ToString()))
                                        {
                                            k = dr.Length;
                                            Log.WriteLog("处理第" + j + "条数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                                            DateTime dtt = DateTime.Now;
                                            //匹配数据后首先根据当前节点进行插入数据
                                            SYS_Express_Word WordModel = new SYS_Express_Word();
                                            WordModel.EW_CaseId = Caseiddt.Rows[j]["CaseId"].ToString();
                                            WordModel.EW_Content = Caseiddt.Rows[j]["ParagraphText"].ToString();
                                            WordModel.EW_UseCount = 1;
                                            WordModel.EW_WordCount = Caseiddt.Rows[j]["ParagraphText"].ToString().Length;
                                            WordModel.ParagraphTypeId = Convert.ToInt32(Caseiddt.Rows[j]["ParagraphTypeId"].ToString());
                                            WordModel.EW_Right = 0;
                                            WordModel.EW_Wrong = 0;
                                            computerNode(WordModel, conditon.SituationId, Caseiddt.Rows[j]["ParagraphText"].ToString());
                                            //wordbll.AddWord_Process(WordModel);
                                            CurrentCount++; // 显示进度条    
                                            if (MaxCount >= CurrentCount)
                                            {
                                                processShow(MaxCount, CurrentCount);
                                            }
                                            lockObj.WaitOne();
                                            if (readyToClose)
                                            {
                                            }
                                            TimeSpan ts = DateTime.Now - dtt;
                                            Log.WriteLog("处理第" + j + "条数据完毕" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 耗时：" + ts.Seconds + "秒");
                                            continue;
                                            //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）
                                        }
                                        else
                                        {
                                            CurrentCount++; // 显示进度条
                                            if (MaxCount >= CurrentCount)
                                            {
                                                processShow(MaxCount, CurrentCount);
                                            }
                                            lockObj.WaitOne();
                                            if (readyToClose)
                                            {
                                            }
                                        }
                                    }
                                }
                                else
                                {

                                    CurrentCount++; // 显示进度条
                                    if (MaxCount >= CurrentCount)
                                    {
                                        processShow(MaxCount, CurrentCount);
                                    }
                                    lockObj.WaitOne();
                                    if (readyToClose)
                                    {
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(ex.Message);
                            }
                        }
                    }
                }

                #region MyRegion
                // }
                //else


                //if (/*conditon.SituationName == "争议焦点要素" &&*/ !conditon.IsHasOwnNode)
                //{
                //    dic.Clear();
                //    //先获取数据然后再删除
                //    expressWordData = MatchSituationbll.GetExpressWordData(conditon.SituationId);
                //    MatchSituationbll.DeleteSys_ExpressAndWordZhengYI(conditon.SituationId);
                //    string msID = MatchSituationbll.ExistsMatchSituationByCCID(conditon.SituationId);
                //    // DataTable dt = casecausebll.Select_parentNode_child(conditon.SituationId);
                //    caseTable = casecausebll.GetAllChild(conditon.SituationId);
                //    regexCodeTable = expressSourcebll.GetRegexInfoCodeByCCID(conditon.SituationId);
                //    for (int j = 0; j < expressWordData.Rows.Count; j++)
                //    {
                //        if (!dic.Contains(expressWordData.Rows[j]["EW_CaseId"].ToString()))
                //        {

                //            dic.Add(expressWordData.Rows[j]["EW_CaseId"].ToString());
                //            //for (int k = 0; k < caseTable.Rows.Count; k++)
                //            //{
                //            DataRow[] dr = regexCodeTable.Select(" CC_ID ='" + conditon.SituationId + "'");
                //            if (dr.Length > 0)
                //            {
                //                for (int k1 = 0; k1 < dr.Length; k1++)
                //                {
                //                    //判断是否匹配
                //                    if (getregex.IsMatch(dr[k1]["CC_RegexInfoCode"].ToString(), expressWordData.Rows[j]["EW_Content"].ToString()))
                //                    {
                //                        Log.WriteLog("处理第" + j + "条数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                //                        DateTime dtt = DateTime.Now;
                //                        k1 = dr.Length;
                //                        //匹配数据后首先根据当前节点进行插入数据
                //                        SYS_Express_Word WordModel = new SYS_Express_Word();
                //                        WordModel.MS_ID = msID;
                //                        WordModel.EW_CaseId = expressWordData.Rows[j]["EW_CaseId"].ToString();
                //                        WordModel.EW_Content = expressWordData.Rows[j]["EW_Content"].ToString();
                //                        WordModel.EW_UseCount = 1;
                //                        WordModel.EW_WordCount = expressWordData.Rows[j]["EW_Content"].ToString().Length;
                //                        WordModel.ParagraphTypeId = Convert.ToInt32(expressWordData.Rows[j]["ParagraphTypeId"].ToString());
                //                        // WordModel.EW_ID = wordbll.Exists_WordProcess(WordModel);
                //                        WordModel.EW_IsSingleExpress = 1;

                //                        WordModel.MS_ID = conditon.SituationId;
                //                        WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                //                        WordModel.EW_Right = 0;
                //                        WordModel.EW_Wrong = 0;
                //                        computerNode(WordModel, conditon.SituationId, expressWordData.Rows[j]["EW_Content"].ToString());
                //                        CurrentCount++; // 显示进度条
                //                        if (MaxCount >= CurrentCount)
                //                        {
                //                            processShow(MaxCount, CurrentCount);
                //                        }
                //                        lockObj.WaitOne();
                //                        if (readyToClose)
                //                        {
                //                        }
                //                        TimeSpan ts = DateTime.Now - dtt;
                //                        Log.WriteLog("处理第" + j + "条数据完毕" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 耗时：" + ts.Seconds + "秒");

                //                        continue;

                //                        //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）

                //                    }
                //                }
                //            }
                //            //string res = expressSourcebll.GetRegexInfoCodeByExpressSource(dt.Rows[k]["CC_ID"].ToString());                           
                //        }
                //        else
                //        {
                //            CurrentCount++; // 显示进度条
                //            if (MaxCount >= CurrentCount)
                //            {
                //                processShow(MaxCount, CurrentCount);
                //            }
                //            lockObj.WaitOne();
                //            if (readyToClose)
                //            {
                //            }
                //        }
                //    }
                //    //}
                //}
                //else
                //{
                //    dic.Clear();
                //    string parentId = casecausebll.GetParentIDByID(conditon.SituationId);
                //    //先获取数据然后再删除
                //    expressWordData = MatchSituationbll.GetExpressWordData(parentId);
                //    MatchSituationbll.DeleteSys_ExpressNotZhengYi(parentId);
                //    string msID = MatchSituationbll.ExistsMatchSituationByCCID(parentId);
                //    // DataTable dt = casecausebll.Select_parentNode_child(casecausebll.GetParentIDByID(conditon.SituationId));
                //    caseTable = casecausebll.GetAllChild(parentId);
                //    regexCodeTable = expressSourcebll.GetRegexInfoCodeByCCID(parentId);
                //    // string res = expressSourcebll.GetRegexInfoCodeByExpressSource(parentId);
                //    for (int j = 0; j < expressWordData.Rows.Count; j++)
                //    {
                //        if (!dic.Contains(expressWordData.Rows[j]["EW_CaseId"].ToString()))
                //        {
                //            dic.Add(expressWordData.Rows[j]["EW_CaseId"].ToString());
                //            DataRow[] dr = regexCodeTable.Select(" CC_ID ='" + parentId + "'");
                //            if (dr.Length > 0)
                //            {
                //                for (int k1 = 0; k1 < dr.Length; k1++)
                //                {
                //                    //判断是否匹配
                //                    if (getregex.IsMatch(dr[k1]["CC_RegexInfoCode"].ToString(), expressWordData.Rows[j]["EW_Content"].ToString()))
                //                    {
                //                        Log.WriteLog("处理第" + j + "条数据" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                //                        DateTime dtt = DateTime.Now;
                //                        k1 = dr.Length;
                //                        //匹配数据后首先根据当前节点进行插入数据
                //                        SYS_Express_Word WordModel = new SYS_Express_Word();
                //                        WordModel.MS_ID = string.IsNullOrEmpty(msID) ? casecausebll.GetParentIDByID(conditon.SituationId) : msID;
                //                        WordModel.EW_CaseId = expressWordData.Rows[j]["EW_CaseId"].ToString();
                //                        WordModel.EW_Content = expressWordData.Rows[j]["EW_Content"].ToString();
                //                        WordModel.EW_UseCount = 1;
                //                        WordModel.EW_WordCount = expressWordData.Rows[j]["EW_Content"].ToString().Length;
                //                        WordModel.ParagraphTypeId = Convert.ToInt32(expressWordData.Rows[j]["ParagraphTypeId"].ToString());
                //                        // WordModel.EW_ID = wordbll.Exists_WordProcess(WordModel);
                //                        WordModel.EW_IsSingleExpress = 1;
                //                        //if (WordModel.EW_ID == "")
                //                        //{
                //                        WordModel.MS_ID = casecausebll.GetParentIDByID(conditon.SituationId);
                //                        WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                //                        WordModel.EW_Right = 0;
                //                        WordModel.EW_Wrong = 0;
                //                        computerNode(WordModel, casecausebll.GetParentIDByID(conditon.SituationId), expressWordData.Rows[j]["EW_Content"].ToString());
                //                        //   wordbll.AddWord_Process(WordModel);
                //                        //}
                //                        //else
                //                        //{
                //                        CurrentCount++; // 显示进度条
                //                        if (MaxCount >= CurrentCount)
                //                        {
                //                            processShow(MaxCount, CurrentCount);
                //                        }
                //                        lockObj.WaitOne();
                //                        if (readyToClose)
                //                        {
                //                        }
                //                        TimeSpan ts = DateTime.Now - dtt;
                //                        Log.WriteLog("处理第" + j + "条数据完毕" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 耗时：" + ts.Seconds + "秒");

                //                        continue;
                //                        // }
                //                        //根据下级节点进行匹配数据（只有父级挂上关系，才会去匹配子级）
                //                    }
                //                }
                //            }
                //        }
                //        else
                //        {
                //            CurrentCount++; // 显示进度条
                //            if (MaxCount >= CurrentCount)
                //            {
                //                processShow(MaxCount, CurrentCount);
                //            }
                //            lockObj.WaitOne();
                //            if (readyToClose)
                //            {
                //            }
                //        }
                //    }

                //}
                //  } 
                #endregion

            }
            catch (Exception ex)
            {
                Log.WriteLog("民事跑表述新错误：" + ex.Message);
                throw new Exception(ex.Message);
            }

        }

        public List<string> li = new List<string>();

        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="WordModel"></param>
        /// <param name="ccID"></param>
        /// <param name="paragraphText"></param>
        public void computerNode(SYS_Express_Word WordModel, string ccID, string paragraphText)
        {

            try
            {
                //根据节点获取他的子节点
                //DataTable dt = casecausebll.Select_parentNode_child(ccID);
                DataRow[] dr = caseTable.Select("CC_ParentID='" + ccID + "'");
                if (dr.Length > 0)
                {
                    bool result = false;
                    for (int i = 0; i < dr.Length; i++)
                    {
                        int ccNodeType = Convert.ToInt32(caseTable.Select(" CC_ID ='" + dr[i]["CC_ID"].ToString() + "'")[0]["CC_NodeType"].ToString());
                        //string res = expressSourcebll.GetRegexInfoCodeByExpressSource(dr[i]["CC_ID"].ToString());
                        DataRow[] dr1 = regexCodeTable.Select(" CC_ID ='" + dr[i]["CC_ID"].ToString() + "'");
                        if (dr1.Length > 0)
                        {
                            for (int k = 0; k < dr1.Length; k++)
                            {
                                //匹配
                                try
                                {
                                    if (getregex.IsMatch(dr1[k]["CC_RegexInfoCode"].ToString(), paragraphText))
                                    {
                                        result = true;
                                        k = dr1.Length;
                                        //WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                                        //WordModel.MS_ID = dr[i]["CC_ID"].ToString();
                                        //16争议焦点 17裁判理由 22裁判结果 从裁判理由开始进行单一和组合的匹配
                                        DataRow[] dr2 = caseTable.Select("CC_ParentID='" + dr[i]["CC_ID"].ToString() + "'");
                                        if (dr2.Length > 0)
                                        {
                                            int ccNodeType1 = Convert.ToInt32(dr2[0]["CC_NodeType"].ToString());
                                            if (ccNodeType == 16 && ccNodeType1 == 17)
                                            {
                                                computerNode2(WordModel, dr[i]["CC_ID"].ToString(), paragraphText);
                                            }
                                            else if (ccNodeType == 22 && ccNodeType1 == 17)
                                            {
                                                computerNode3(WordModel, dr[i]["CC_ID"].ToString(), paragraphText);
                                            }
                                            else
                                            {
                                                computerNode(WordModel, dr[i]["CC_ID"].ToString(), paragraphText);
                                            }
                                        }
                                        else
                                        {
                                            AddToExpress(dr[i]["CC_ID"].ToString(), WordModel, paragraphText, caseTable.Select("CC_ID='" + dr[i]["CC_ID"].ToString() + "'")[0]["AllParentId"].ToString(), caseTable.Select("CC_ID='" + dr[i]["CC_ID"].ToString() + "'")[0]["CC_ParentID"].ToString());
                                        }


                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.WriteLog("民事新跑表述发生错误：" + "正则匹配发生错误：" + dr1[k]["CC_RegexInfoCode"].ToString() + "ccID:" + dr1[k]["CC_ID"].ToString() + ex.Message);
                                    System.Windows.Forms.MessageBox.Show("正则匹配发生错误：" + dr1[k]["CC_RegexInfoCode"].ToString() + "ccID:" + dr1[k]["CC_ID"].ToString() + ex.Message);
                                }
                                //else
                                //{
                                //    continue;
                                //if (casecausebll.IsLastNode(dr[i]["CC_ID"].ToString()) && i + 1 == dr.Count)
                                //{
                                //    WordModel.MS_ID = dr[i]["CC_ParentID"].ToString();

                                //}
                                //else
                                //{
                                //    WordModel.MS_ID = dr[i]["CC_ID"].ToString();
                                //}
                                //string res1 = expressSourcebll.GetRegexInfoCodeByExpressSource(WordModel.MS_ID);
                                //if (getregex.IsMatch(res1.ToString(), paragraphText))
                                //{
                                //    WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                                //    string str = wordbll.Exists_WordProcess(WordModel);
                                //    bool result = wordbll.IsExistsData(WordModel);
                                //    if (string.IsNullOrEmpty(str) && !result)
                                //    {
                                //        wordbll.AddWord_Process(WordModel);
                                //    }
                                //}
                                //}
                                //}
                            }
                        }

                    }
                    if (!result)
                    {
                        AddToExpress(ccID, WordModel, paragraphText, caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString(), caseTable.Select("CC_ID='" + ccID + "'")[0]["CC_ParentID"].ToString());
                    }
                }
                else
                {
                    AddToExpress(ccID, WordModel, paragraphText, caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString(), caseTable.Select("CC_ID='" + ccID + "'")[0]["CC_ParentID"].ToString());
                    //WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                    //string res1 = expressSourcebll.GetRegexInfoCodeByExpressSource(WordModel.MS_ID);
                    //if (getregex.IsMatch(res1.ToString(), paragraphText))
                    //{
                    // WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                    //string str = wordbll.Exists_WordProcess(WordModel);
                    //bool result = wordbll.IsExistsData(WordModel);
                    //if (string.IsNullOrEmpty(str) && !result)
                    //{CC_ParentID
                    //  WordModel.MS_ID= caseTable.Select("CC_ID='" + ccID + "'")[0]["CC_ParentID"].ToString();
                    // wordbll.AddWord_Process(WordModel);
                    // }
                    //}
                }

            }
            catch (Exception ex)
            {
                Log.WriteLog("民事跑表述新错误：" + ex.Message);
                throw new Exception(ex.Message);
            }


        }




        /// <summary>
        /// 处理数据(争议焦点)
        /// </summary>
        /// <param name="WordModel"></param>
        /// <param name="ccID"></param>
        /// <param name="paragraphText"></param>
        public void computerNode2(SYS_Express_Word WordModel, string ccID, string paragraphText)
        {

            try
            {
                //根据节点获取他的子节点
                //DataTable dt = casecausebll.Select_parentNode_child(ccID);
                StringBuilder sbCode = new StringBuilder();
                List<string> commonName = new List<string>();
                List<string> comCode = new List<string>();
                DataRow[] dr = caseTable.Select("CC_ParentID='" + ccID + "'");
                if (dr.Length > 0)
                {
                    //bool result = false;
                    for (int i = 0; i < dr.Length; i++)
                    {
                        //int ccNodeType = Convert.ToInt32(caseTable.Select(" CC_ID ='" + dr[i]["CC_ID"].ToString() + "'")[0]["CC_NodeType"].ToString());
                        //string res = expressSourcebll.GetRegexInfoCodeByExpressSource(dr[i]["CC_ID"].ToString());
                        DataRow[] dr1 = regexCodeTable.Select(" CC_ID ='" + dr[i]["CC_ID"].ToString() + "'");
                        if (dr1.Length > 0)
                        {
                            for (int k = 0; k < dr1.Length; k++)
                            {
                                //匹配
                                try
                                {
                                    if (getregex.IsMatch(dr1[k]["CC_RegexInfoCode"].ToString(), paragraphText))
                                    {
                                        if (!commonName.Contains(dr[i]["CC_ID"].ToString()))
                                        {
                                            commonName.Add(dr[i]["CC_ID"].ToString());
                                        }
                                        break;
                                        #region MyRegion
                                        //result = true;
                                        //k = dr1.Length;
                                        ////WordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                                        ////WordModel.MS_ID = dr[i]["CC_ID"].ToString();
                                        ////16争议焦点 17裁判理由
                                        //DataRow[] dr2 = caseTable.Select("CC_ParentID='" + dr[i]["CC_ID"].ToString() + "'");
                                        //if (dr2.Length > 0)
                                        //{
                                        //    int ccNodeType1 = Convert.ToInt32(dr2[0]["CC_NodeType"].ToString());
                                        //    if (ccNodeType == 16 && ccNodeType1 == 17)
                                        //    {
                                        //        computerNode2(WordModel, dr[i]["CC_ID"].ToString(), paragraphText);
                                        //    }
                                        //    else
                                        //    {
                                        //        computerNode(WordModel, dr[i]["CC_ID"].ToString(), paragraphText);
                                        //    }
                                        //}
                                        //else
                                        //{
                                        //    AddToExpress(dr[i]["CC_ID"].ToString(), WordModel, paragraphText);
                                        //} 
                                        #endregion
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.WriteLog("民事新跑表述发生错误：" + "正则匹配发生错误：" + dr1[k]["CC_RegexInfoCode"].ToString() + "ccID:" + dr1[k]["CC_ID"].ToString() + ex.Message);
                                    System.Windows.Forms.MessageBox.Show("正则匹配发生错误：" + dr1[k]["CC_RegexInfoCode"].ToString() + "ccID:" + dr1[k]["CC_ID"].ToString() + ex.Message);
                                }
                            }
                        }

                    }
                    #region MyRegion
                    //if (commonName.Count == 1)
                    //{
                    //    work.AddMatch_virtual1("S" + ccID, "单一", ccID);
                    //    string singleCCID = caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString() + ",S" + ccID;
                    //    string ccChildNode = caseTable.Select("CC_ParentID='" + commonName[0] + "'")[0]["CC_ID"].ToString();
                    //    AddToExpress(commonName[0]+","+ ccChildNode, WordModel, paragraphText, singleCCID, "S" + ccID);
                    //}
                    //else 
                    #endregion
                    if (commonName.Count > 0)
                    {
                        //name是裁判结果类型-支持原告-支持被告
                        string name = string.Empty;
                        DataRow[] drs = caseTable.Select("CC_ParentID='" + commonName[0] + "'");
                        if(drs.Length>0)
                        {
                            name = drs[0]["CC_Name"].ToString();
                        }                      
                        comCode.Add(commonName[0]);
                        commonName.Remove(commonName[0]);
                        for (int i = 0; i < commonName.Count; i++)
                        {
                            DataRow[] drs1 = caseTable.Select("CC_ParentID='" + commonName[i] + "'");
                            if (drs1.Length > 0 && name == drs1[0]["CC_Name"].ToString())
                            {
                                if (!comCode.Contains(commonName[i]))
                                {
                                    //获取第一个裁判理由的裁判结果，后面的如果和第一个相同，则组成一个集合
                                    comCode.Add(commonName[i]);
                                    commonName.RemoveAt(i);
                                }
                            }
                        }
                        if (comCode.Count > 1 || commonName.Count > 1)
                        {
                            work.AddMatch_virtual1("S" + ccID, "单一", ccID);
                            work.AddMatch_virtual1("M" + ccID, "组合", ccID);
                            string singleCCID = caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString() + ",S" + ccID;
                            string doubleCCID = caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString() + ",M" + ccID;
                            #region MyRegion
                            //if (comCode.Count == 1)
                            //{
                            //    string ccChildNode = caseTable.Select("CC_ParentID='" + comCode[0] + "'")[0]["CC_ID"].ToString();
                            //    AddToExpress(comCode[0] + "," + ccChildNode, WordModel, paragraphText, singleCCID, "S" + ccID);
                            //    AddToExpress(comCode[0], WordModel, paragraphText, singleCCID, "S" + ccID);
                            //}
                            //if (commonName.Count == 1)
                            //{
                            //    string ccChildNode = caseTable.Select("CC_ParentID='" + commonName[0] + "'")[0]["CC_ID"].ToString();

                            //    AddToExpress(commonName[0] + "," + ccChildNode, WordModel, paragraphText, singleCCID, "S" + ccID);
                            //}
                            #endregion
                            if (comCode.Count == 1)
                            {
                                string ccChildNode = caseTable.Select("CC_ParentID='" + comCode[0] + "'")[0]["CC_ID"].ToString();
                                //AddToExpress(comCode[0] + "," + ccChildNode, WordModel, paragraphText, singleCCID, "S" + ccID);
                                AddToExpress(ccChildNode, WordModel, paragraphText, "-1", comCode[0]);
                            }
                            else if (commonName.Count == 1)
                            {
                                string ccChildNode = caseTable.Select("CC_ParentID='" + commonName[0] + "'")[0]["CC_ID"].ToString();
                                AddToExpress(ccChildNode, WordModel, paragraphText, "-1", commonName[0]);
                            }
                            if (comCode.Count > 1)
                            {
                                StringBuilder sbDouble = new StringBuilder();
                                foreach (var item in comCode)
                                {
                                    sbDouble.Append(item + ",");
                                }
                                AddToExpress(sbDouble.ToString(), WordModel, paragraphText, doubleCCID, "M" + ccID);
                            }
                            if (commonName.Count > 1)
                            {
                                StringBuilder sbDouble = new StringBuilder();
                                foreach (var item in commonName)
                                {
                                    sbDouble.Append(item + ",");
                                }
                                AddToExpress(sbDouble.ToString(), WordModel, paragraphText, doubleCCID, "M" + ccID);
                            }

                        } else
                        {
                             if (comCode.Count == 1)
                            {
                                string ccChildNode = caseTable.Select("CC_ParentID='" + comCode[0] + "'")[0]["CC_ID"].ToString();
                                //AddToExpress(comCode[0] + "," + ccChildNode, WordModel, paragraphText, singleCCID, "S" + ccID);
                                AddToExpress(ccChildNode, WordModel, paragraphText, "-1", comCode[0]);
                            }
                              if (commonName.Count == 1)
                            {
                                string ccChildNode = caseTable.Select("CC_ParentID='" + commonName[0] + "'")[0]["CC_ID"].ToString();
                                AddToExpress(ccChildNode, WordModel, paragraphText, "-1", commonName[0]);
                            } 
                        }
                    }
                    else if (commonName.Count == 0)//节点的子集都没有匹配上 则挂到父节点上面
                    {
                        AddToExpress(ccID, WordModel, paragraphText, caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString(), caseTable.Select("CC_ID='" + ccID + "'")[0]["CC_ParentID"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("民事跑表述新错误：" + ex.Message);
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// 处理数据（理由要素）
        /// </summary>
        /// <param name="WordModel"></param>
        /// <param name="ccID"></param>
        /// <param name="paragraphText"></param>
        public void computerNode3(SYS_Express_Word WordModel, string ccID, string paragraphText)
        {

            try
            {
                //根据节点获取他的子节点
                //DataTable dt = casecausebll.Select_parentNode_child(ccID);
                StringBuilder sbCode = new StringBuilder();
                List<string> commonName = new List<string>();
                List<string> comCode = new List<string>();
                DataRow[] dr = caseTable.Select("CC_ParentID='" + ccID + "'");
                if (dr.Length > 0)
                {
                    //bool result = false;
                    for (int i = 0; i < dr.Length; i++)
                    {
                        DataRow[] dr1 = regexCodeTable.Select(" CC_ID ='" + dr[i]["CC_ID"].ToString() + "'");
                        if (dr1.Length > 0)
                        {
                            for (int k = 0; k < dr1.Length; k++)
                            {
                                //匹配
                                try
                                {
                                    if (getregex.IsMatch(dr1[k]["CC_RegexInfoCode"].ToString(), paragraphText))
                                    {
                                        if (!commonName.Contains(dr[i]["CC_ID"].ToString()))
                                        {
                                            commonName.Add(dr[i]["CC_ID"].ToString());
                                        }
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.WriteLog("民事新跑表述发生错误：" + "正则匹配发生错误：" + dr1[k]["CC_RegexInfoCode"].ToString() + "ccID:" + dr1[k]["CC_ID"].ToString() + ex.Message);
                                    System.Windows.Forms.MessageBox.Show("正则匹配发生错误：" + dr1[k]["CC_RegexInfoCode"].ToString() + "ccID:" + dr1[k]["CC_ID"].ToString() + ex.Message);
                                }
                            }
                        }

                    }
                    if (commonName.Count > 1)
                    {
                        work.AddMatch_virtual1("S" + ccID, "单一", ccID);
                        work.AddMatch_virtual1("M" + ccID, "组合", ccID);
                        string doubleCCID = caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString() + ",M" + ccID;
                        StringBuilder sbDouble = new StringBuilder();
                        foreach (var item in commonName)
                        {
                            sbDouble.Append(item + ",");
                        }
                        AddToExpress(sbDouble.ToString(), WordModel, paragraphText, doubleCCID, "M" + ccID);
                    }
                    else if (commonName.Count == 1)
                    {
                        AddToExpress(commonName[0], WordModel, paragraphText, "-1", "M" + ccID);

                    }
                    else if (commonName.Count == 0)
                    {
                        AddToExpress(ccID, WordModel, paragraphText, caseTable.Select("CC_ID='" + ccID + "'")[0]["AllParentId"].ToString(), caseTable.Select("CC_ID='" + ccID + "'")[0]["CC_ParentID"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("民事跑表述新错误：" + ex.Message);
                throw new Exception(ex.Message);
            }
        }

        #region 入库
        /// <summary>
        /// 添加表述
        /// </summary>
        /// <param name="CaseCauseID">案由小分类ID</param>
        /// <param name="ParagraphID">逻辑段140，150</param>
        /// <param name="resultText">索引句子</param>
        /// <param name="CC_IDList">ID字符串</param>
        /// <param name="caseid">案例ID</param>
        public void AddToExpress(string CC_IDList, SYS_Express_Word WordModel, string paragraphText, string allParentId, string parentId)
        {
            #region 添加match表
            //民事只有认定不认定
            #region 节点属性
            bool affirmIdeaResultb = getregex.IsMatch("EI1601201151020001", paragraphText);//True 2 不认定 False 1 认定
            string affirmIdeaResult = affirmIdeaResultb ? "2" : "1";
            var stiuModel = new SYS_Express_MatchSituation
            {
                IsMatch = 0,
                MatchSource = "0",
                AffirmIdeaResult = affirmIdeaResult,
                AffirmIdeaSource = "0"
            };

            #endregion

            string msId = "";
            //组合情形
            string[] situationIDa = CC_IDList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //2017-07-13 ljj 修改
            if (situationIDa.Length == 1)
            {
                msId = work.AddSingleMatch1(caseTable.Select(" CC_ID ='" + situationIDa[0] + "'")[0], situationIDa[0], situationIDa[0], stiuModel, "-1", parentId);//从0开始
            }
            else if (situationIDa.Length > 1)
            {
                string allCombinePid = situationIDa[0];
                //string allMsIdPid = parentId;

                msId = work.AddCivilMatch1(caseTable.Select(" CC_ID ='" + situationIDa[0] + "'")[0], situationIDa[0], allCombinePid, stiuModel, allParentId, parentId);//从0开始
                for (int i = 1; i < situationIDa.Length; i++)
                {

                    if (allParentId != "")
                        allParentId += "," + msId;


                    msId = work.AddCivilMatch1(caseTable.Select(" CC_ID ='" + situationIDa[i] + "'")[0], situationIDa[i], allCombinePid, stiuModel, allParentId, msId);
                }
            }
            #endregion

            #region 添加文本

            SYS_Express_Word wordModel = new SYS_Express_Word();
            wordModel.MS_ID = msId;
            wordModel.EW_CaseId = WordModel.EW_CaseId;
            wordModel.EW_Content = WordModel.EW_Content;
            wordModel.EW_UseCount = 1;
            wordModel.EW_WordCount = WordModel.EW_WordCount;
            wordModel.ParagraphTypeId = WordModel.ParagraphTypeId;

            if (situationIDa.Length == 1) wordModel.EW_IsSingleExpress = 1;
            else wordModel.EW_IsSingleExpress = 0;

            wordModel.EW_ID = wordbll.Exists_WordProcess(wordModel);

            if (wordModel.EW_ID == "")
            {
                wordModel.EW_ID = newid.JudgeDoc_CreateID("SYS_Express_Word");
                wordModel.EW_Right = 0;
                wordModel.EW_Wrong = 0;

                wordbll.AddWord_Process(wordModel);
            }
            else
            {
                wordbll.UpdateWord_Process(wordModel);
            }
            #endregion

        }
        #endregion

































        public void computerNode(string ewID, string ccID, string paragraphText, string EW_CaseId)
        {

            try
            {
                //根据节点获取他的子节点
                DataTable dt = casecausebll.Select_parentNode_child(ccID);
                StringBuilder sb = new StringBuilder();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string res = expressSourcebll.GetRegexInfoCodeByExpressSource(dr["CC_ID"].ToString());
                        //string[] str = res.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        //for (int i = 0; i < str.Length; i++)
                        //{
                        //匹配
                        if (getregex.IsMatch(res.ToString(), paragraphText))
                        {
                            computerNode(ewID, dr["CC_ID"].ToString(), paragraphText, EW_CaseId);
                            sb.Append(dr["CC_ID"].ToString() + ",");
                            wordbll.UpdateWord_ProcessIsSingleExpress(1, dr["CC_ID"].ToString(), ewID);
                        }
                        //}
                    }
                }
                string[] res1 = sb.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (res1.Length > 1)
                {
                    Log.WriteLog("组合节点：" + sb.ToString() + " caseID:" + EW_CaseId + " 匹配文本：" + paragraphText);
                    wordbll.UpdateWord_ProcessIsSingleExpress(0, ccID, ewID);
                }
                else if (res1.Length == 1)
                {
                    dt = casecausebll.Select_parentNode_child(res1[0].ToString());
                    //有子级，递归插入
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        computerNode(ewID, res1[0].ToString(), paragraphText, EW_CaseId);
                    }
                    else//没有子级，修改数据
                    {
                        Log.WriteLog("单一节点：" + sb.ToString() + " caseID:" + EW_CaseId + " 匹配文本：" + paragraphText);

                        wordbll.UpdateWord_ProcessIsSingleExpress(1, res1[0].ToString(), ewID);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }


        }

        //
        /// <summary>
        /// 加工情形、情节
        /// </summary>
        /// <param name="AllPid"></param>
        /// <param name="inputText"></param>
        /// <param name="headwordid"></param>
        /// <param name="CC_NodeType"></param>
        /// <param name="caseid"></param>
        public void execute_People(string inputText, string caseid, string ParagraphID, string CaseCauseID, DataTable conditiondt)//第一层情形情节（conditiondt限定规则范围）
        {
            List<string> resultlist = new List<string>();
            IList<string> list = new List<string>();
            string CC_RegexInfoCode = "";
            string CC_ID = "";
            string CC_Name = "";
            int Reg_Count = 0;
            int ChildCount = 0;
            List<string> MatchList = new List<string>();
            List<string> DeletePidlist = new List<string>();//父级待删除的关系
            MatchList.Add(inputText);
            Dictionary<string, List<string>> AllResultText = new Dictionary<string, List<string>>(); //总结果集
            DataRow[] arrayDR = conditiondt.Select("CC_ParentID='" + CaseCauseID + "'");//分组情形
            for (int j = 0; j < arrayDR.Length; j++)//第一级
            {
                CC_RegexInfoCode = arrayDR[j]["CC_RegexInfoCode"].ToString();
                CC_ID = arrayDR[j]["CC_ID"].ToString();
                CC_Name = arrayDR[j]["CC_Name"].ToString();
                Reg_Count = Convert.ToInt32(arrayDR[j]["RegCount"].ToString());
                ChildCount = Convert.ToInt32(arrayDR[j]["ChildCount"].ToString());
                MatchChild(conditiondt, MatchList, ref DeletePidlist, CC_ID, CC_RegexInfoCode, Reg_Count, ChildCount, ref AllResultText, CaseCauseID);//匹配
            }
            foreach (KeyValuePair<string, List<string>> item in AllResultText)
            {
                foreach (var str in item.Value)
                {
                    work.AddToExpress(str, caseid, ParagraphID, CaseCauseID, item.Key);
                }
            }
        }
        #region 加工处理

        /// <summary>
        /// 匹配
        /// </summary>
        /// <param name="conditiondt">节点数据</param>
        /// <param name="OneinputText">父级的数据集</param>
        /// <param name="CC_ID">情形ID</param>
        /// <param name="CC_RegexInfoCode">情形编号</param>
        /// <param name="Reg_Count">当前节点的正则个数</param>
        /// <param name="ChildCount">当前节点的子集个数</param>
        /// <param name="AllResultText">总匹配结果集</param>
        /// <param name="Pid">父ID</param>
        public void MatchChild(DataTable conditiondt, List<string> OneinputText, ref List<string> DeletePidlist, string CC_ID, string CC_RegexInfoCode, int Reg_Count, int ChildCount, ref Dictionary<string, List<string>> AllResultText, string Pid)//CC_ID 及以下是不是有匹配
        {
            List<string> MatchList = new List<string>();//节点匹配结果
            if (Reg_Count > 0)
            {
                MatchList = ISMatch(OneinputText, CC_RegexInfoCode, ref DeletePidlist);
            }
            else
            {
                MatchList = OneinputText;
            }
            if (MatchList.Count > 0)//有结果
            {
                if (!AllResultText.ContainsKey(CC_ID))
                {
                    AllResultText.Add(CC_ID, MatchList); //保存结果
                }
                //继续处理下级
                if (ChildCount > 0)// 有子集
                {
                    List<string> Deletelist = new List<string>();//节点本身待删除的关系
                    DataRow[] arrayDR = conditiondt.Select("CC_ParentID='" + CC_ID + "' and CC_NodeType<>12");
                    for (int j = 0; j < arrayDR.Length; j++)
                    {
                        MatchChild(conditiondt, MatchList, ref Deletelist, arrayDR[j]["CC_ID"].ToString(), arrayDR[j]["CC_RegexInfoCode"].ToString(), Convert.ToInt32(arrayDR[j]["RegCount"].ToString()), Convert.ToInt32(arrayDR[j]["ChildCount"].ToString()), ref AllResultText, CC_ID);
                    }

                    if (AllResultText.ContainsKey(CC_ID) && AllResultText[CC_ID].Count > 0)
                    {
                        foreach (var item in Deletelist)
                        {
                            AllResultText[CC_ID].Remove(item);
                        }
                        if (AllResultText[CC_ID].Count == 0)
                        {
                            AllResultText.Remove(CC_ID);
                        }
                    }
                }
            }
        }

        //提取
        public List<string> ISMatch(List<string> inputText, string CC_RegexInfoCode, ref List<string> DeletePidlist)
        {
            List<string> Resultlist = new List<string>();
            List<string> newlist = new List<string>();
            foreach (var item in inputText)//父级提取的列表
            {
                newlist = (List<string>)getregex.GetParseList(CC_RegexInfoCode, item);
                //自己保存结果集
                foreach (var str in newlist)
                {
                    if (!Resultlist.Contains(str))
                        Resultlist.Add(str);
                }
                // 删掉父级的原文本
                if (newlist.Count > 0)
                {
                    DeletePidlist.Add(item);//父级待删
                }
            }
            return Resultlist; //匹配 CC_RegexInfoCode为编号     
        }
        #endregion


        #endregion

    }

    //public class Ew
    //{
    //    public string Ew_Content { get; set; }

    //    public int Ew_WordCount { get; set; }
    //}

}
